<template>
    <header class="header">
        <span @click.stop="handleMenu" class="menu">
            <ThMenu v-if="menuflag"></ThMenu>
            <ThClose v-else></ThClose>

        </span>
        <span class="logo">ACMING</span>
        <nav class="navbar">
            <ul>
                <li><router-link to="/layout/home">首页</router-link></li>
                <li><router-link to="/layout/city">城市</router-link></li>
                <li><router-link to="/layout/spot">景区</router-link></li>
                <li><router-link to="/layout/tourist">酒店住宿</router-link></li>
                <li><router-link to="/layout/article">文章<b></b></router-link>
                    <ul>
                        <li @click="handleRouter(item.id)" v-for="item in categoryList" :key="item.id"><a>{{ item.name
                                }}</a></li>
                    </ul>
                </li>
                <li><router-link to="/layout/about">关于</router-link></li>
                <li><router-link to="/layout/contract">联系我们</router-link></li>
                <li><router-link to="/layout/image">图片</router-link></li>
            </ul>
        </nav>
        <div class="icons">
            <BsCartFill class="cart" />
            <el-dropdown @command="handleCommand" :hide-on-click="false" v-if="avatar">
                <span class="el-dropdown-link">
                    <img :src="avatar">
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item :icon="UserFilled" command="userInfo">个人信息</el-dropdown-item>
                        <el-dropdown-item :icon="SwitchButton" divided command="logout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
            <FaUserLarge v-else @click="handleUser" class="user" />
        </div>

    </header>
</template>

<script setup>
import { ThMenu, BsCartFill, FaUserLarge, ThClose } from '@kalimahapps/vue-icons';
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router'
import { UserFilled, SwitchButton } from '@element-plus/icons-vue';
import { getCategoryListAPI } from '@/api'
import { useUserInfoStore } from '@/store/userInfo/index.js'
import { logoutaAPI } from '@/api'

const $router = useRouter()

const menuflag = ref(true)
const handleMenu = () => {
    let naverBar = document.querySelector(".navbar")
    if (menuflag.value == false) {
        naverBar.classList.remove('active')
    }
    else {
        naverBar.classList.add('active')
    }
    menuflag.value = !menuflag.value
}

const handleUser = () => {
    $router.push({
        path: '/login',
    }).catch(err => { })
}

let categoryList = ref()
const initCategoryList = async () => {
    const res = await getCategoryListAPI()
    categoryList.value = res
}

const handleRouter = (val) => {
    $router.push({
        path: "/layout/article",
        query: {
            categoryId: val
        }
    }).catch(err => { })
}
initCategoryList()

const avatar = ref()

watch(() => useUserInfoStore().userInfo,
    () => {
        console.log("popoppo", localStorage.getItem("userStore"))
        avatar.value = useUserInfoStore().userInfo.avatar
    }, { deep: true, immediate: true })

const handleCommand = (val) => {
    if (val == 'logout') {
        logout()
    }
    else {
        $router.push({
            path: "/person",

        }).catch(err => { })
    }
}
const logout = async () => {
    const res = await logoutaAPI()
    useUserInfoStore().reset()
    localStorage.clear();
}
</script>

<style lang="scss" scoped>
.header {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    background-color: white;
    box-shadow: 0 .5rem 1rem rgba(0, 0, 0, 0.2);
    padding: 0 9%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 100;
}

.header .logo {
    color: #222;
    font-size: 2.2rem;
    font-weight: bolder;
}

.header ul {
    position: relative;
}

.header ul li {
    position: relative;
    list-style: none;
    float: left;

    .router-link-active {
        color: orange;
    }
}

.header ul li a {
    color: #222;
    font-size: 1.75rem;
    padding: 20px 15px;
    display: flex;
    justify-content: space-between;
    cursor: pointer;
}

.header ul li a:hover {
    color: orange;
}


.header ul li ul {
    position: absolute;
    left: 0;
    width: 200px;
    background-color: #eee;
    display: none;
}

.header ul li:hover ul {
    display: block;
}

.header ul li:hover ul li:hover a {
    background-color: rgba(0, 0, 0, 0.1);
}

.header ul li ul li {
    position: relative;
    width: 100%;
}

.header .icons {
    display: flex;
    align-items: center;
}

.header .icons .user,
.cart {
    cursor: pointer;
    font-size: 2.2rem;
    color: #222;
    margin-left: 2rem;
}

.el-dropdown-link {
    margin-left: 2rem;
    cursor: pointer;
    display: flex;
    text-align: center;
    align-items: center;

    img {
        cursor: pointer;
        height: 4rem;
        width: 4rem;
        border-radius: 50%;
    }
}

:deep(.el-tooltip__trigger:focus-visible) {
    outline: unset;
}


.header .menu {
    cursor: pointer;
    font-size: 2.2rem;
    color: #222;
    display: none;
    margin-right: 2rem;
}


@media (max-width:1200px) {
    .header {
        padding: 0 2rem;
    }
}



@media(max-width:768px) {
    .header {
        padding: 2rem;
    }

    .header .menu {
        display: block;
        transition: .2s linear;
    }

    .header nav {
        position: absolute;
        background: white;
        top: 100%;
        right: 0;
        transition: .2s linear;
        left: 0;
        clip-path: polygon(0 0, 100% 0, 100% 0, 0 0);
    }

    .header .active {
        clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
    }

    .header nav a {
        display: inline-block;
        text-align: center;
        font-size: 1.8rem;
    }

    .header nav ul li {
        width: 100%;
    }

    .header nav ul li ul {
        position: relative;
        width: 100%;
        left: 0;

    }

    .header ul li a {
        justify-content: center;
    }

}
</style>